import * as THREE from "three";
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";


var w = window.innerWidth
var h = window.innerHeight
var scene, camera, renderer, vertices = [], colors = [], light, mesh
const LEN = 600000, SIZE = 1
var SCOPE = 180

function main() {
  init()
  setLine()
  setLight()
}
function init() {
  // 创建场景对象Scene
  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(75, w / h, 1, 1000)
  camera.position.x = 0
  camera.position.y = 0
  camera.position.z = 320
  camera.up.x = 0
  camera.up.y = 1
  camera.up.z = 0
  camera.lookAt({ x: 0, y: 0, z: 0 })

  renderer = new THREE.WebGLRenderer()
  renderer.setClearColor('#8edee2')
  renderer.setSize(w, h)
  document.body.appendChild(renderer.domElement)
}
function setLine() {
  // let object = new THREE.Object3D()
  let lanse = new THREE.TextureLoader().load('/static/lanse.jpeg')
  let huise = new THREE.TextureLoader().load('/static/huise.jpeg')
  new OBJLoader().load(
    '/static/male.obj',
    (object)=>{
      object.traverse((child)=>{
        if(child.isMesh){
          if(child.name === "mesh1.002_mesh1-geometry_male-02-1noCullingID_male-02-1noCulling.JP"){
            child.material.map = huise
          }if(child.name === 'mesh4.002_mesh4-geometry__01_-_Default1noCulli__01_-_Default1noCulli'){
            child.material.map = lanse
          }
        }
      })
      object.position.y -= 100
      scene.add(object)
    }
  )
}

function setLight() {
  var light2 = new THREE.DirectionalLight(0xffff00, 0.6)
  light2.position.set(0, 0, -800)
  scene.add(light2)

  light = new THREE.DirectionalLight(0xffff00, 0.7, 1000, 1)
  light.position.set(0, 0, 800)
  scene.add(light)
}
function render() {
  
  renderer.render(scene, camera)
}

main()
render()
// /** --------------------鼠标控件对象--------------------- */
let controls = new OrbitControls(camera, renderer.domElement); //创建控件对象
controls.addEventListener("change", render); //监听鼠标、键盘事件